package com.bosssoft.boss_exam_db.service.busines.impl;

import com.bosssoft.boss_exam_db.entity.Applicant;
import com.bosssoft.boss_exam_db.entity.ResumeParameter;
import com.bosssoft.boss_exam_db.mapper.ApplicantMapper;
import com.bosssoft.boss_exam_db.service.base.impl.BaseServiceImpl;
import com.bosssoft.boss_exam_db.service.busines.ApplicantService;
import com.bosssoft.boss_exam_db.service.busines.ResumeParameterService;
import com.bosssoft.boss_exam_db.util.MySqlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author cherish
 */
@Service
@Slf4j
@Transactional
public class ApplicantServiceImpl extends BaseServiceImpl<Applicant> implements ApplicantService {

    //注入应聘人操作mapper
    @Autowired
    private ApplicantMapper applicantMapper;

    //注入简历操作mapper
    @Autowired
    private ResumeParameterService resumeParameterService;

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/14
     * @Description 根据应聘人的姓名、学历、毕业学校 查询用户信息
     * @Param [username, education, school, page, limit, sort, order]
     * @Return com.github.pagehelper.PageInfo<com.bosssoft.boss_exam_db.entity.Applicant>
     * @Exception //TODO
     * @since JDK 1.8
     **/
    @Override
    public PageInfo<Applicant> queryByCondition(String username, String education, String school, Integer page, Integer limit, String sort, String order) {
        Example example = new Example(Applicant.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(username)) {
            criteria.andLike("username", MySqlUtil.cloumLike(username));
        }
        if (!StringUtils.isEmpty(education)) {
            criteria.andEqualTo("education", education);
        }
        if (!StringUtils.isEmpty(school)) {
            criteria.andLike("school", MySqlUtil.cloumLike(school));
        }
        criteria.andEqualTo("deleted", false);
        PageInfo<Applicant> applicantPageInfo = PageHelper.startPage(page, limit).setOrderBy(sort + " " + order).doSelectPageInfo(() ->
                this.applicantMapper.selectByExample(example)
        );
        return applicantPageInfo;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 删除应聘人和简历
     * @Param [applicant]
     * @Return java.lang.Boolean
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public Boolean deleteApplicantAndResume(Applicant applicant) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
            int i = this.deleteForLogic(applicant);
            Example example = new Example(ResumeParameter.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("applicantId", applicant.getId());
            List<ResumeParameter> resumeParameters = resumeParameterService.selectByExample(example);
            for(ResumeParameter r:resumeParameters){
                resumeParameterService.deleteForLogic(new ResumeParameter());
            }
            return true;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 获取应聘人ID
     * @Param [applicantId]
     * @Return com.bosssoft.boss_exam_db.entity.Applicant
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public Applicant getApplicantById(String applicantId) {
        Applicant applicant = this.applicantMapper.selectByPrimaryKey(applicantId);
        if(applicant!=null && applicant.getDeleted()==false){
            return applicant;
        }else {
            return null;
        }
    }

}
